/*
 * @Date: 2022-03-04 11:07:20
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc: 
 * @LastEditTime: 2022-05-18 15:01:26
 * @FilePath: \web\src\store\modules\user.js
 */
 
import { login } from "@/core/api/module/BaseLogin";
import { reloadinfo } from "@/core/api/module/user";

import { createRouteByList } from "@/core/route/permission";
import { message } from "ant-design-vue";

const state = {
  onlineUser:[],
  token: localStorage.getItem("USER_TOKEN") != null ? localStorage.getItem("USER_TOKEN") : "", 
  userInfo: localStorage.getItem('USER_INFO') != null ? JSON.parse(localStorage.getItem('USER_INFO')) : null,
  userRoutes: localStorage.getItem("USER_ROUTES") != null ? JSON.parse(localStorage.getItem("USER_ROUTES"))  : [], 
  tabs: localStorage.getItem("USER_TABS") != null ? JSON.parse(localStorage.getItem("USER_TABS")) : {list:[],active:{path:"",query:""}},  //标签页
}

const mutations = {
  SET_ONLINE_USER(state,users){
    state.onlineUser = users
  },
  SET_USER_TOKEN(state, token) {
    if (token) {
      state.token = token;
      localStorage.setItem('USER_TOKEN', token);
    } else {
      state.token = '';
      localStorage.removeItem('USER_TOKEN')
    }
  },
  SET_USER_INFO(state, userInfo) { 
    if (userInfo) {
      state.userInfo = userInfo
      localStorage.setItem('USER_INFO', JSON.stringify(userInfo))
    } else {
      state.userInfo = '';
      localStorage.removeItem('USER_INFO')
    }
    
  },
  SET_USER_MENU(state, menuList) {  
    if (menuList) { 
      //删除时间字段
      menuList.forEach(item => {
        delete item.create_time;
        delete item.update_time;
      })
      state.userRoutes = menuList
      localStorage.setItem('USER_ROUTES', JSON.stringify(menuList))
    } else {  
      state.userRoutes = []
      localStorage.removeItem('USER_ROUTES')
    } 
  },
  SET_USER_TABS(state, tabs) {
    if (tabs) {
      state.tabs = tabs
      localStorage.setItem('USER_TABS', JSON.stringify(tabs))
    } else {
      state.tabs = []
      localStorage.removeItem('USER_TABS')
    }
  }

}

const actions = {
  setUserToken({commit}, token) {
    return new Promise(resolve => {
      commit('SET_USER_TOKEN', token);
      resolve()
    })
  },
  async logout({commit}) {  
    message.success("注销成功", 0.5).then(function(){
      commit('SET_USER_TOKEN');
      commit('SET_USER_INFO');
      commit('SET_USER_MENU');
       window.location.reload();
    });
     return Promise.resolve();
  },
  async login({commit}, data) {
    try {
      const response = await login(data)
      const {code, msg, data: userInfo} = response 
      if (code === 0) {
        const { token,permissions } = userInfo 
        delete userInfo.permissions
        delete userInfo.token  
        commit('SET_USER_TOKEN', token)
        commit('SET_USER_INFO', userInfo)
        commit('SET_USER_MENU', permissions)  //缓存list路由
        return Promise.resolve(userInfo)
      } else { 
        return Promise.reject(msg)
      }
    } catch (e) { 
      console.log(e)
    }
  }, 
  async reloadInfo({commit}) {
    try {
      const response = await reloadinfo()
      const {code, msg, data: userInfo} = response 
      if (code === 0) {
        const { permissions } = userInfo 
        delete userInfo.permissions
        delete userInfo.token  
        commit('SET_USER_INFO', userInfo)
        commit('SET_USER_MENU', permissions)  //缓存list路由
        message.success("clear success", 0.5).then(function(){
          window.location.reload();
        });
        return Promise.resolve()
      } else { 
        return Promise.reject(msg)
      }
    } catch (e) { 
      console.log(e)
    }
  }, 
}

export default {
  namespaced: true,
  mutations,
  actions,
  state
}